package com.system.manager.modules.projectLibrary.controller;


import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.common.annotation.PreAuthorize;
import com.system.manager.common.entity.Result;
import com.system.manager.common.exception.SysException;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.enums.SubjectType;
import com.system.manager.modules.projectLibrary.entity.Department;
import com.system.manager.modules.projectLibrary.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.PastOrPresent;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jisf
 * @since 2021-11-03
 */
@Api(tags = "部门API")
@RestController
@RequestMapping("/projectLibrary/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService iDepartmentService;

    @ApiOperation("查询本公司所有部门")
    @PreAuthorize({SubjectType.ENTERPRISE_ADMIN, SubjectType.zx})
    @GetMapping("/select/all/department")
    public Result selectAllDepartment(@CurrSubject Subject subject) {
        try {
            List<Department> departments = iDepartmentService.selectAllDepartment(subject);
            return Result.ok(departments);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("查询本公司所有部门")
    @PreAuthorize({SubjectType.ENTERPRISE_ADMIN, SubjectType.zx})
    @GetMapping("/select/department/by/type")
    public Result selectDepartmentByType(@RequestParam(name = "page", required = false, defaultValue = "1") Integer page,
                                         @RequestParam(name = "limit", required = false, defaultValue = "10") Integer size,
                                         @CurrSubject Subject subject) {
        try {
            Result result = iDepartmentService.selectDepartmentByType(page, size, subject);
            return result;
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("插入部门信息")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @PostMapping("/insert/department")
    public Result insertDepartment(@CurrSubject Subject subject, @RequestBody Department department) {
        try {
            iDepartmentService.insertDepartment(subject, department);
            return Result.ok("添加成功");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

    @ApiOperation("删除部门")
    @PreAuthorize(SubjectType.ENTERPRISE_ADMIN)
    @GetMapping("/delete/department")
    public Result deleteDepartment(@RequestParam Integer id) {
        try {
            iDepartmentService.deleteDepartment(id);
            return Result.ok("删除成功");
        } catch (SysException e) {
            e.printStackTrace();
            return Result.error(e);
        }
    }

}
